package com.suep.business.controller;

import com.suep.business.domain.StoreReview;
import com.suep.business.service.IStoreReviewService;
import com.suep.system.config.BaseR;
import com.suep.system.utils.RequestAttributeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/storeReview")
public class StoreReviewController {

    @Autowired
    private IStoreReviewService storeReviewService;

    @GetMapping("/getById")
    public BaseR<StoreReview> getById(@RequestParam Integer reviewId) {
        return new BaseR<>().ok("查询成功", storeReviewService.selectStoreReviewById(reviewId));
    }

    @GetMapping("/getByStoreId")
    public BaseR<List<StoreReview>> getByStoreId(@RequestParam Integer storeId) {
        return new BaseR<>().ok("查询成功", storeReviewService.selectStoreReviewByStoreId(storeId));
    }

    @GetMapping("/getByUserId")
    public BaseR<List<StoreReview>> getByUserId(HttpServletRequest request) {
        Integer userId=RequestAttributeUtil.getUserIdFromRequest(request);
        return new BaseR<>().ok("查询成功", storeReviewService.selectStoreReviewByUserId(userId));
    }

    @PostMapping("/add")
    public BaseR<Integer> add(@RequestBody StoreReview storeReview) {
        return new BaseR<>().ok("插入成功", storeReviewService.insertStoreReview(storeReview));
    }

    @PutMapping("/change")
    public BaseR<Integer> change(@RequestBody StoreReview storeReview) {
        return new BaseR<>().ok("更新成功", storeReviewService.updateStoreReview(storeReview));
    }

    @DeleteMapping("/removeById")
    public BaseR<Integer> removeById(@RequestParam Integer reviewId) {
        return new BaseR<>().ok("删除成功", storeReviewService.deleteStoreReviewById(reviewId));
    }
}
